import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterproject/mixin/after_first_frame_layout_mixin.dart';
import 'package:flutterproject/page/main/children/share/data/share_bean.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';

class SharePage extends StatefulWidget {
  @override
  State<SharePage> createState() => _SharePageState();
}

class _SharePageState extends State<SharePage> with AfterFirstFrameLayoutMixin {
  ShareBean? shareBean;
  HttpClient client = Get.find<HttpClient>();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color(0xFFC97400),
      width: Get.width,
      height: Get.height,
      child: Stack(
        children: [
          ImageCommon(
            "/image/sharebg",
            width: Get.width,
            height: 1107.width,
            fit: BoxFit.cover,
          ),
          Padding(
            padding: MarginPaddingUtil.symmetric(horizontal: 65),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 102.width,
                ),
                Icon(
                  Icons.chevron_left,
                  color: Colors.white,
                  size: 50.width,
                ).addClickEvent(() => Get.back()),
                SizedBox(
                  height: 515.width,
                ),
                Stack(
                  clipBehavior: Clip.none,
                  children: [
                    Container(
                      padding: MarginPaddingUtil.fromLTRB(
                          left: 48, top: 115, right: 48, bottom: 70),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12.radius),
                        gradient: LinearGradient(
                          colors: [
                            Colors.white,
                            Color(0xFFF7CD8F),
                          ],
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            alignment: Alignment.center,
                            padding: MarginPaddingUtil.all(10),
                            decoration: BoxDecoration(
                                // border: Border.all(
                                //     color: Color(0xFF707070).withOpacity(0.08),
                                //     width: 4.width),
                                ),
                            child: QrImage(
                              data: shareBean?.shareUrl ?? "",
                              size: 250.width,
                              backgroundColor: Colors.white,
                            ),
                          ),
                          56.heightBox,
                          TextCommon(
                            "我的邀请码",
                            size: 24.sp,
                            lineHeight: 30.sp,
                            color: Color(0xFFA5640B),
                          ),
                          22.heightBox,
                          Container(
                            width: Get.width,
                            padding: MarginPaddingUtil.symmetric(
                                vertical: 10, horizontal: 15),
                            decoration: BoxDecoration(
                              color: Color(0xFFF8D4A0),
                              borderRadius: BorderRadius.circular(12.radius),
                            ),
                            child: TextCommon(
                              shareBean?.shareCode ?? "",
                              size: 28.sp,
                              lineHeight: 30.sp,
                              fontWeight: FontWeight.bold,
                              color: Color(0xFF242429),
                            ),
                          ),
                          31.heightBox,
                          TextCommon(
                            "我的邀请链接",
                            size: 24.sp,
                            lineHeight: 30.sp,
                            color: Color(0xFFA5640B),
                          ),
                          15.heightBox,
                          Container(
                            width: Get.width,
                            padding: MarginPaddingUtil.symmetric(
                                vertical: 10, horizontal: 15),
                            decoration: BoxDecoration(
                              color: Color(0xFFEEC58A),
                              borderRadius: BorderRadius.circular(12.radius),
                            ),
                            child: TextCommon(
                              shareBean?.shareUrl ?? "",
                              size: 24.sp,
                              lineHeight: 30.sp,
                              color: Color(0xFF242429),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Positioned(
                      top: -65.height,
                      right: -60.width,
                      child: ImageCommon(
                        "/image/ic_coin2",
                        width: 183.width,
                        height: 172.height,
                      ),
                    )
                  ],
                ),
                60.heightBox,
                Center(
                  child: ButtonCommon(
                    width: 320.width,
                    text: "复制邀请链接",
                    onTap: () async {
                      await Clipboard.setData(
                          ClipboardData(text: shareBean?.shareUrl ?? ""));
                      ToastUtil.showSuccess("复制成功!");
                    },
                    fontSize: 32.sp,
                    fontColor: Colors.white,
                    color: Color(0xFFEDC88B),
                    borderRadios: 50,
                    padding: MarginPaddingUtil.symmetric(vertical: 17),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Future afterFirstFrameLayout(BuildContext context) async {
    try {
      DialogUtil.showLoading();
      shareBean = await client.get<ShareBean>(AddressManager.share);
      setState(() {});
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
    } finally {
      DialogUtil.dismiss();
    }
  }
}
